package com.xuecheng.system.controller;

import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.vo.AuthInfo;
import com.xuecheng.commons.model.vo.CompanyVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.AuthInfoHolder;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.commons.utils.JwtUtils;
import com.xuecheng.system.domain.Company;
import com.xuecheng.web.exception.BusinessException;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xuecheng.system.service.CompanyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 * 教育机构前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
public class CompanyController {

    @Autowired
    private CompanyService  companyService;
    //机构查询
    @GetMapping("/company/mine")
    public ResponseResult<CompanyVo> findCompany(@RequestHeader("Authorization") String token){//取出请求头
/*        //校验token合法性
        Boolean verifyToken = JwtUtils.verifyToken(token);
        if (verifyToken==false){
            throw new BusinessException(ErrorCode.NOTTOKEN);
        }
        //解析token 取出
        AuthInfo authInfo = JwtUtils.getInfoFromToken(token);*/
        // 1.获取线程中登录信息
        AuthInfo authInfo = AuthInfoHolder.getAuthInfo();
        //判断登录用户是否为企业用户
        if (authInfo.getCompanyId()==null) {//如果不是企业用户 就没有companyId
            throw new BusinessException(ErrorCode.COMPANYERROR);
        }
        //根据企业id查询企业
        Company company = companyService.getById(authInfo.getCompanyId());
        //将企业信息转实体VO
       /* CompanyVo companyVo = new CompanyVo();
        BeanUtils.copyProperties(company,companyVo);*/
        CompanyVo companyVo = BeanHelper.copyProperties(company, CompanyVo.class);
        //返回实体结果
        return ResponseResult.okResult(companyVo);


    }

}
